user=$1
access_token=$2
echo "user: $user"
echo "access_token: $access_token"
if [ "$user" = "" ];then
        echo "ussage: ./this_script user access_token"
        exit
fi

if [ "$access_token" = "" ];then
	echo "ussage: ./this_script user access_token"
	exit
fi
url="https://gitee.com/api/v5/user/repos?access_token=""$access_token""&visibility=all&affiliation=owner&sort=full_name&direction=asc&page=1&per_page=100"
#echo "url: $url"
result=`curl -X GET --header 'Content-Type: application/json;charset=UTF-8' $url`
adds=`echo $result | grep  -o -E "\"https://gitee.com/$user/(([a-z]|[A-Z]|[_]|[-]|[0-9]){1,20})"`
#echo "adds: $adds"
repos=''
for address in $adds;
do
       repo=${address##*/}
       #echo "repo: $repo"
       already_have=false
       #echo "already_have: $already_have"
       for repo_in_repos in $repos
       do
	      #echo "repo_in_repos: $repo_in_repos"
	       if [ "$repo_in_repos" = "$repo" ];then
		       already_have=true
		       #echo "already_have: $already_have"
		fi
       done
       if [ "$already_have" = "false" ];then
       		repos+="$repo " 
       		#echo "$repo"
       fi
       #echo "                 "
done
if [ "$repos" = "" ];then
        echo "There is no repos got, perhaps wrong access_token or there's no repo in this account."
        exit
fi
for repo in $repos; 
do 
	echo ${repo}
	cd ./${repo}
	if [ $? -ne 0 ] ; then
		git clone git@gitee.com:lishaojing/${repo}.git
	else
		git pull   
		cd ..
	fi
done
